package com.clss.commandhelper.function.newEvidence;

import android.util.Log;

import com.clss.commandhelper.base.BaseCallback;
import com.clss.commandhelper.base.BaseJson;
import com.clss.commandhelper.base.BasePresenter;
import com.clss.commandhelper.base.BaseView;
import com.clss.commandhelper.net.ApiConstants;
import com.clss.commandhelper.tools.UploadHelper;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/11/23.
 */

public class EvidenceAndNewCasePresenter extends BasePresenter {

    CaseView caseView;
    EvidenceAndNewModel model;

    public void setCaseView(CaseView caseView) {
        this.caseView = caseView;
    }

    public void upLoadFile(List<TaskPoliceFile> files,InfoTaskDispatch infoTaskDispatch){
        caseView.showLoadingDialog();
        if (model == null)
            model = new EvidenceAndNewModel(caseView.getActContext());
        List<TaskPoliceFile> taskPoliceFiles = new ArrayList<>();
        if (files == null || files.size() == 0){
            upEvidenceMessage(infoTaskDispatch);
        }
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).getName() == null){
                taskPoliceFiles.add(files.get(i));
                if (taskPoliceFiles.size() == files.size()) {
                    infoTaskDispatch.setPoliceFile(taskPoliceFiles);
                    upEvidenceMessage(infoTaskDispatch);
                }
                continue;
            }
            File file = new File(files.get(i).getName());
            UploadHelper.uploadEvidenceFile(files.get(i), file, ApiConstants.getUploadEvidenceUrl(),
                    new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            caseView.dismissLoadingDialog();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String responseBody = response.body().string();
                            Log.e("responseBody",responseBody);
                            try {
                                BaseJson baseJson = mGson.fromJson(responseBody, BaseJson.class);

                                if (baseJson.getCode() == 201) {
                                    TaskPoliceFile taskPoliceFile = new TaskPoliceFile();
                                    JsonElement jsonElement = (JsonElement) baseJson.getList().get(0);
                                    JsonObject jsonObject = jsonElement.getAsJsonObject();
                                    taskPoliceFile.setFileId(jsonObject.get("id").getAsString());
                                    taskPoliceFile.setName(jsonObject.get("name").getAsString());
                                    taskPoliceFile.setType(jsonObject.get("type").getAsInt());
                                    taskPoliceFile.setAddress(jsonObject.get("address").getAsString());
                                    taskPoliceFile.setLatlng(jsonObject.get("latlng").getAsString());
                                    taskPoliceFile.setRecordTimeLength(jsonObject.get("audioTime").getAsInt());
                                    taskPoliceFile.setTime(jsonObject.get("time").getAsString());
                                    taskPoliceFiles.add(taskPoliceFile);

                                    if (taskPoliceFiles.size() == files.size()) {
                                        infoTaskDispatch.setPoliceFile(taskPoliceFiles);
                                        upEvidenceMessage(infoTaskDispatch);
                                    }
                                } else {
                                    caseView.responseError(baseJson.getCode(), baseJson.getError());
                                }

                            } catch (JsonSyntaxException e) {
                                caseView.responseError(200,"解析失败");
                            }
                        }
                    });
        }
    }

    public void upEvidenceMessage(InfoTaskDispatch infoTaskDispatch){
        if (model == null)
            model = new EvidenceAndNewModel(caseView.getActContext());
        model.postEvidenceMessage(infoTaskDispatch, new BaseCallback(caseView) {
            @Override
            public void responseSuccess(BaseJson baseJson) {
                if (baseJson.getObj() != null && !baseJson.getObj().toString().equals("") &&
                        !baseJson.getObj().toString().equals("null")){
                    String string = baseJson.getObj().toString();
                    String s = string.substring(1, string.length() - 1);
                    String[] ids =s.split(",");
                    caseView.postSuccess(ids[0],ids[1]);
                }else {
                    caseView.postSuccess(null,null);
                }
            }

            @Override
            public void onFailure(retrofit2.Call<BaseJson> call, Throwable t) {
                caseView.postFailure();
            }
        });
    }

    @Override
    public void cancelCall() {

    }


    interface CaseView extends BaseView {
        void postSuccess(String taskId,String dispatchId);
        void postFailure();

        int getType();
    }
}
